package com.the_last.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.the_last.pojo.dto.SysPermissionDTO;
import com.the_last.pojo.po.SysPermission;
import com.the_last.service.ISysPermissionService;
import com.the_last.utils.Result;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/permission")
@RequiredArgsConstructor
public class SysPermissionController {
    
    private final ISysPermissionService permissionService;
    
    /**
     * 分页获取权限列表
     */
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('PERMISSION_QUERY')")
    public Result<Page<SysPermission>> getPermissionList(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String resourceType,
            @RequestParam(required = false) Integer status,
            @RequestParam(required = false) Long businessTypeId) {
        
        SysPermissionDTO queryDTO = new SysPermissionDTO();
        queryDTO.setResourceType(resourceType);
        queryDTO.setStatus(status);
        queryDTO.setBusinessTypeId(businessTypeId);
        
        Page<SysPermission> page = new Page<>(current, size);
        return permissionService.getPermissionList(page, queryDTO);
    }
    
    /**
     * 获取用户权限列表
     */
    @GetMapping("/user/{userId}")
    @PreAuthorize("hasAuthority('PERMISSION_QUERY')")
    public Result<List<SysPermission>> getPermissionsByUserId(@PathVariable Long userId) {
        return permissionService.getPermissionsByUserId(userId);
    }
    
    /**
     * 新增权限
     */
    @PostMapping("/add")
    @PreAuthorize("hasAuthority('PERMISSION_CREATE')")
    public Result<Void> addPermission(@Valid @RequestBody SysPermissionDTO permissionDTO) {
        SysPermission permission = new SysPermission();
        BeanUtils.copyProperties(permissionDTO, permission);
        return permissionService.addPermission(permission);
    }
    
    /**
     * 更新权限
     */
    @PutMapping("/update")
    @PreAuthorize("hasAuthority('PERMISSION_UPDATE')")
    public Result<Void> updatePermission(@Valid @RequestBody SysPermissionDTO permissionDTO) {
        if (permissionDTO.getId() == null) {
            return Result.fail("权限ID不能为空");
        }
        
        SysPermission permission = new SysPermission();
        BeanUtils.copyProperties(permissionDTO, permission);
        return permissionService.updatePermission(permission);
    }
    
    /**
     * 删除权限
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('PERMISSION_DELETE')")
    public Result<Void> deletePermission(@PathVariable Long id) {
        // TODO 可能有逻辑问题
        return permissionService.deletePermission(id);
    }
    
    /**
     * 根据ID获取权限详情
     */
    @GetMapping("/{id}")
    @PreAuthorize("hasAuthority('PERMISSION_QUERY')")
    public Result<SysPermission> getPermissionById(@PathVariable Long id) {
        SysPermission permission = permissionService.getById(id);
        if (permission == null || permission.getDeleted().equals(1)) {
            return Result.fail("权限不存在");
        }
        return Result.success(permission);
    }
    
}